package com.hwua.course.mybatis;

import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.Connection;

/*********************************************************************************
//* Copyright (C) 2018 com.Hwua All Rights Reserved.
//*
//* Filename:      JdbcOrig.java
//* Revision:      1.0
//* Author:        lyh
//* Created On:    2018年10月2日
//* Modified by:   
//* Modified On:   
//*
//* Description:   <description>
/********************************************************************************/

public class JdbcOrig {

    public static void main(String[] args) {

        ResultSet rs = null;
        PreparedStatement ps = null;
        Connection connection = null;

        try {
            // 1 获取数据库连接
            // 1.1 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 1.2 通过驱动获得数据库连接
            connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8", "root", "123456");

            // 2 执行增、删、改、查询
            // 2.1 定义SQL语句，？表示占位符  新增
            String sql = "insert into user(username,birthday,sex,address) values(?,?,?,?)";
            // 2.2 预编译sql语句
            // 
            ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            // 2.3 对应占位符，给出具体的执行语句
            ps.setString(1, "wangshi");

            ps.setDate(2, Date.valueOf("1998-08-09"));
            // ps.setDate(2, new Date(99, 2, 11));

            ps.setString(3, "2");

            ps.setString(4, "科京大厦6楼T7");

            // 3 结果
            int num = ps.executeUpdate();
            rs = ps.getGeneratedKeys();
            if (rs.next())
                System.out.println("生成的键值为：" + rs.getInt(1));

            if (num > 0)
                System.out.println("插入数据成功");

            // 进行数据的查询
            //            String queryString = "select * from user where username = ?";
            //            ps = connection.prepareStatement(queryString);
            //            ps.setString(1, "zhengliu");
            //            rs = ps.executeQuery();
            //            if (rs.next()) {
            //                System.out.println(rs.getString("id") + "  " + rs.getString("username"));
            //            }

        }
        catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
        catch (SQLException e2) {
            e2.printStackTrace();
        }
        finally {
            // 4 释放资源
            if (null != rs) {
                try {
                    rs.close();
                }
                catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != ps) {
                try {
                    ps.close();
                }
                catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != connection) {
                try {
                    connection.close();
                }
                catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
